package com.bw.usercenter.presenter

import com.bw.mvpcore.BaseRepository
import com.bw.net.protocol.resp.BaseRespEntity

import com.bw.usercenter.contract.UserCenterContract
import com.bw.usercenter.model.protocol.req.UserEntitiy
import com.bw.usercenter.model.protocol.resp.*
import com.bw.usercenter.repositoty.UserCenterRepositoryImpl
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

class UserCenterPresenterImpl(view:UserCenterContract.UserCenterView): UserCenterContract.UserCenterPresenter(view) {
    override fun register(enity: UserEntitiy) {
        mRepository.register(enity)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseRespEntity<RespUserEntity>>{
                override fun onSubscribe(d: Disposable) {

                }

                override fun onNext(t: BaseRespEntity<RespUserEntity>) {
                    mView.get()!!.registerSuccess(t)
                }

                override fun onError(e: Throwable) {
                    mView.get()!!.registerFaild(e)
                }

                override fun onComplete() {

                }
            })
    }

    override fun login(enity: UserEntitiy) {
        mRepository.login(enity)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<BaseRespEntity<RespUserEntity>>{
                override fun onSubscribe(d: Disposable) {
                }

                override fun onNext(t: BaseRespEntity<RespUserEntity>) {
                    println("-----成功")
                    mView.get()!!.registerSuccess(t)

                }

                override fun onError(e: Throwable) {
                    println("-----失败")
                    mView.get()!!.registerFaild(e)
                }

                override fun onComplete() {
                }

            })
    }

    override fun requestTB(page: Int) {
        mRepository.requestTB(page)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<RespGoodEntity>{
                override fun onSubscribe(d: Disposable) {

                }

                override fun onNext(t: RespGoodEntity) {
                    if (t.code==200){
                        println("-----成功")
                        mView.get()!!.registerSuccess(t)
                    }

                }

                override fun onError(e: Throwable) {
                    println("-----失败${e.message}")
                    mView.get()!!.registerFaild(e)
                }

                override fun onComplete() {

                }
            })
    }

    override fun getauthcode() {
        mRepository.getauthcode()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<Int>{
                override fun onSubscribe(d: Disposable) {

                }

                override fun onNext(t: Int) {
                    mView.get()!!.registerSuccess(t)
                }

                override fun onError(e: Throwable) {
                    mView.get()!!.registerFaild(e)
                }

                override fun onComplete() {

                }
            })
    }

    override fun getRecomment(){
        mRepository.getRecomment()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<RespRecommendEntity>{
                override fun onSubscribe(d: Disposable) {

                }

                override fun onNext(t: RespRecommendEntity) {
                    mView.get()!!.registerSuccess(t)
                }

                override fun onError(e: Throwable) {
                    mView.get()!!.registerFaild(e)
                }

                override fun onComplete() {

                }

            })
    }

    override fun getSerch(name: String) {
        println("---$name")
        mRepository.getSrarch(name)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<RespSearchEntity>{
                override fun onSubscribe(d: Disposable) {

                }

                override fun onNext(t: RespSearchEntity) {
                    println("")
                    mView.get()!!.registerSrech(t)
                }

                override fun onError(e: Throwable) {
                    mView.get()!!.registerFaild(e)
                }

                override fun onComplete() {

                }

            })

    }


    override fun getSearchitem(itemid: String) {
        mRepository.getSearchitem(itemid)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object :Observer<RespSearchitemIDEntity>{
                override fun onSubscribe(d: Disposable) {

                }

                override fun onNext(t: RespSearchitemIDEntity) {
                    mView.get()?.getSearchitem(t)
                }

                override fun onError(e: Throwable) {
                    mView.get()!!.registerFaild(e)
                }

                override fun onComplete() {

                }
            })
    }


    override fun createPresenter(): UserCenterContract.UserCenterRepository {
        return UserCenterRepositoryImpl()
    }
}


